package com.bbg.amyam.web.mvc.cachemanager;

import java.util.HashMap;
import java.util.List;

import com.bbg.amyam.domain.model.School;
import com.bbg.amyam.web.mvc.exception.NoSchoolFoundException;

public class SchoolCacheManager {

	private static HashMap<String,School> schools=new HashMap<String,School>();
	public static School getSchool(String schoolShortName) throws NoSchoolFoundException {
		School school=findSchool(schoolShortName);
		if(school==null){
			throw new NoSchoolFoundException("No school found for short name:"+schoolShortName);
			//school=findSchool("default"); 
		}
		return school;
	}

	private static School findSchool(String schoolShortName) {
		School school=schools.get(schoolShortName);
		if(school==null){
			List<School> schoolList = School.findSchoolsByShortNameEquals(schoolShortName).getResultList();
			if(schoolList.size()>0){
				school=schoolList.get(0);
				//schools.put(schoolShortName, school);
			}
		}
		return school;
	}

}
